<?php

class KeuanganController extends Controller
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout='//layouts/template';

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
			'postOnly + delete', // we only allow deletion via POST request
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('index','view','create','update','LaporanKeuangan','LaporanLabaRugi','LaporanBukuBesar','LaporanShu','TutupBuku'),
				'users'=>array('*'),
			),
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array(),
				'users'=>array('@'),
			),
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('admin','delete'),
				'users'=>array('admin'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}
	
	public function actionLaporanKeuangan()
	{
		if(isset($_POST['tanggal_awal'])&&isset($_POST['tanggal_akhir'])){
			
			$tanggal['awal'] = date("Y-m-d",strtotime($_POST['tanggal_awal']));
			$tanggal['akhir'] = date("Y-m-d",strtotime($_POST['tanggal_akhir']));
			
			$tanggal_awal = $tanggal['awal'];
			$tanggal_akhir = $tanggal['akhir'];
			
			for($i=1;$i<4;$i++){

				$akun = MasterAkun::model()->findAll("ID_KEL_AKUN=".$i." OR ID_KEL_AKUN LIKE '".$i."%'");
				
				$data []= $this->countJurnal($akun,$tanggal);
			}
			
			$bayar = PembayaranPinjaman::model()->findAll("TANGGAL_TRANSAKSI BETWEEN '".$tanggal_awal."' AND '".$tanggal_akhir."'");
		
			foreach ($bayar as $databayar){
				$dataBayar [] = $this->countBunga($databayar->ID_PINJAMAN,$databayar->JUMLAH_BAYAR);
			}
			
			$asalpinjaman = AsalPinjaman::model()->findAll();
			foreach($asalpinjaman as $each){
				$bungaPinjaman = 0;
				foreach($dataBayar as $datum){
					if ($datum["id_asal"] == $each->ID_ASAL_PINJAMAN){
						$bungaPinjaman = $bungaPinjaman + $datum["selisih_bunga"];
					}
				}
				$dataBunga []= array("asal"=>$each->ASAL_PINJAMAN,"bunga"=>$bungaPinjaman);
			}
			
			$akun_pendapatan = MasterAkun::model()->findAll("ID_KEL_AKUN='4' OR ID_KEL_AKUN LIKE '4%'");
			$dataPendapatan= $this->countLabaRugi($akun_pendapatan,$tanggal);
			
			$akun_beban = MasterAkun::model()->findAll("ID_KEL_AKUN='5' OR ID_KEL_AKUN LIKE '5%'");
			$dataBeban= $this->countLabaRugi($akun_beban,$tanggal);
			
			$jumlah_pendapatan=0;$jumlah_beban=0;
			
			foreach($dataBunga as $eachBunga){
				$jumlah_pendapatan = $jumlah_pendapatan + $eachBunga["bunga"];
			}
			foreach($dataPendapatan as $eachPendapatan){
				$jumlah_pendapatan = $jumlah_pendapatan + $eachPendapatan["JUMLAH"];
			}
			foreach($dataBeban as $eachBeban){
				$jumlah_beban = $jumlah_beban + $eachBeban["JUMLAH"];
			}
			
			$labarugi = $jumlah_pendapatan-$jumlah_beban;
			
			
			$objPHPExcel = new PHPExcel();
			$sheet = $objPHPExcel->setActiveSheetIndex(0);  
			
			$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(10);
			$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(40);
			$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(15);
			
			
			$sheet->mergeCells('A1:D1')->setCellValue('A1', 'LAPORAN KEUANGAN')->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
			$sheet->mergeCells('A2:D2')->setCellValue('A2', 'KOPERASI PEGAWAI RSU HAJI SURABAYA')->getStyle('A2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

			$sheet->mergeCells('A3:D3')->setCellValue('A3', 'NERACA PER : '.$_POST['tanggal_awal']." s/d ".$_POST['tanggal_akhir'])->getStyle('A3')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
			
			$sheet->getStyle("A1:A3")->applyFromArray(array("font" => array( "bold" => true, "size"=>18)));
			$sheet->getStyle("A")->applyFromArray(array("font" => array( "bold" => true)));
			
			$j = 5;$sumHutang=0;
			for ($i=0;$i<3;$i++){
				$sum = 0;
				if($i==0){
					$sheet->setCellValue('A'.$j, 'AKTIVA');
					$j++;
				}
				if ($i==1){
					$sheet->setCellValue('A'.$j, 'PASIVA');
					$j++;
					$sheet->setCellValue('B'.$j, 'HUTANG')->getStyle('B'.$j)->applyFromArray(array("font" => array( "bold" => true)));
					$j++;
					$sheet->setCellValue('B'.$j, 'HUTANG LANCAR :')->getStyle('B'.$j)->applyFromArray(array("font" => array( "bold" => true)));
					$j++;
					$sheet->setCellValue('B'.$j, "* Dana Sosial");
					$sheet->setCellValue('D'.$j, $labarugi*0.05);
					$sumHutang = $sumHutang + $labarugi*0.05;
					$j++;
					$sheet->setCellValue('B'.$j, "* Dana Kesejahteraan");
					$sheet->setCellValue('D'.$j, $labarugi*0.05);
					$sumHutang = $sumHutang + $labarugi*0.05;
					$j++;
					$sheet->setCellValue('B'.$j, "* Dana Pendidikan");
					$sheet->setCellValue('D'.$j, $labarugi*0.05);
					$sumHutang = $sumHutang + $labarugi*0.05;
					$j++;
					$j++;
					$j++;
					$sheet->setCellValue('B'.$j, 'HUTANG JANGKA PANJANG :')->getStyle('B'.$j)->applyFromArray(array("font" => array( "bold" => true)));
					$j++;
					$sheet->setCellValue('B'.$j, "");
					$sheet->setCellValue('D'.$j, "");
					// $sumHutang = $sumHutang + $labarugi*0.05;
					$j++;
					$j++;
					$j++;
					
				}
				if ($i==2){
					$sheet->setCellValue('B'.$j, 'MODAL')->getStyle('B'.$j)->applyFromArray(array("font" => array( "bold" => true)));
					$j++;
				}
					foreach($data[$i] as $each){
						$sheet->setCellValue('B'.$j, $each["AKUN"]);
						$sheet->setCellValue('D'.$j, $each["JUMLAH"]);
						$j++;
						$sum = $sum + $each["JUMLAH"];
					}
				if($i==0 || $i==2){
					if($i==2){
						$sum = $sum + $sumHutang;
					}
					$sheet->setCellValue('D'.$j, $sum)->getStyle('D'.$j)->applyFromArray(array("font" => array( "bold" => true),'borders' => array('top' => array('style' => PHPExcel_Style_Border::BORDER_THICK))));
					$j++;
				}
			}
			
			
			
			$filename = 'Laporan Keuangan '.$_POST['tanggal_awal']." - ".$_POST['tanggal_akhir'];
			header('Content-Type: application/vnd.ms-excel');
			header('Content-Disposition: attachment;filename="'.$filename.'.xlsx"');
			header('Cache-Control: max-age=0');
			
			$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);

			$objWriter->save('php://output');
			unset($this->objWriter);
			unset($this->objWorksheet);
			unset($this->objReader);
			unset($this->objPHPExcel);
			exit();
			
		}
		$this->render('form_laporan_keuangan');
	}
	
	public function countJurnal($idx,$tanggal){
		$data=array();
		foreach($idx as $keyx){
			$id_akun = $keyx->ID_AKUN;
			$saldo_awal = $keyx->SALDO_AWAL;
			$model_jurnal = JurnalDetail::model()->with('jurnal','master')->findAll("(jurnal.TANGGAL BETWEEN '".$tanggal['awal']."' AND '".$tanggal['akhir']."') AND t.ID_AKUN=".$id_akun);
			
			$jumlah = $saldo_awal;
			foreach($model_jurnal as $each){
				if($keyx->ID_KEL_AKUN == 1 || ($keyx->ID_KEL_AKUN > 100 && $keyx->ID_KEL_AKUN < 200)){
					if($each->DEBIT_KREDIT == 1){
						$jumlah=$jumlah+$each->NILAI;
					}
					else{
						$jumlah=$jumlah-$each->NILAI;
					}	
				}
				else{
					if($each->DEBIT_KREDIT == 1){
						$jumlah=$jumlah-$each->NILAI;
					}
					else{
						$jumlah=$jumlah+$each->NILAI;
					}	
				}
				
			}
			$array = array(
				"AKUN"=>$keyx->NAMA_AKUN,
				"JUMLAH"=>$jumlah
			);
			$data[]=$array;
		}
		return $data;
	}
	
	public function actionLaporanLabaRugi(){
		if (isset($_POST['tanggal_awal'])&&isset($_POST['tanggal_akhir'])){
			$tanggal_awal =date("Y-m-d", strtotime($_POST['tanggal_awal']));
			$tanggal_akhir =date("Y-m-d", strtotime($_POST['tanggal_akhir']));
			$tanggal['awal']= $tanggal_awal;
			$tanggal['akhir']= $tanggal_akhir;
			
			$bayar = PembayaranPinjaman::model()->findAll("TANGGAL_TRANSAKSI BETWEEN '".$tanggal_awal."' AND '".$tanggal_akhir."'");
		
			foreach ($bayar as $databayar){
				$data [] = $this->countBunga($databayar->ID_PINJAMAN,$databayar->JUMLAH_BAYAR);
			}
			
			$asalpinjaman = AsalPinjaman::model()->findAll();
			foreach($asalpinjaman as $each){
				$bungaPinjaman = 0;
				foreach($data as $datum){
					if ($datum["id_asal"] == $each->ID_ASAL_PINJAMAN){
						$bungaPinjaman = $bungaPinjaman + $datum["selisih_bunga"];
					}
				}
				$dataBunga []= array("asal"=>$each->ASAL_PINJAMAN,"bunga"=>$bungaPinjaman);
			}
			
			$akun_pendapatan = MasterAkun::model()->findAll("ID_KEL_AKUN='4' OR ID_KEL_AKUN LIKE '4%'");
			$dataPendapatan= $this->countLabaRugi($akun_pendapatan,$tanggal);
			
			$akun_beban = MasterAkun::model()->findAll("ID_KEL_AKUN='5' OR ID_KEL_AKUN LIKE '5%'");
			$dataBeban= $this->countLabaRugi($akun_beban,$tanggal);
			
			$objPHPExcel = new PHPExcel();
			$sheet = $objPHPExcel->setActiveSheetIndex(0);  
			
			$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(20);
			$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(40);
			$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(15);
			$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(15);
			
			$sheet->mergeCells('A1:D1')->setCellValue('A1', 'LAPORAN LABA RUGI')->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
			$sheet->mergeCells('A2:D2')->setCellValue('A2', 'KOPERASI PEGAWAI RSU HAJI SURABAYA')->getStyle('A2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

			$sheet->mergeCells('A3:D3')->setCellValue('A3', 'PER : '.$_POST['tanggal_awal']." s/d ".$_POST['tanggal_akhir'])->getStyle('A3')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
			
			$sheet->getStyle("A1:A3")->applyFromArray(array("font" => array( "bold" => true, "size"=>18)));
			$sheet->getStyle("A")->applyFromArray(array("font" => array( "bold" => true)));
			
			$sheet->setCellValue('A6', 'I. SIMPAN PINJAM')->getStyle("A6")->applyFromArray(array("font" => array( "bold" => true)));
			
			$i=7;$jumlah_pendapatan=0;$jumlah_beban=0;
			
			foreach($dataBunga as $eachBunga){
				$sheet->setCellValue('B'.$i, '* Simpan Pinjam Koperasi dari '.$eachBunga["asal"]);
				$sheet->setCellValue('D'.$i, $eachBunga["bunga"]);
				
				$jumlah_pendapatan = $jumlah_pendapatan + $eachBunga["bunga"];
				$i++;
			}
			$i++;
			$sheet->setCellValue('A'.$i, 'II. PENDAPATAN')->getStyle("A".$i)->applyFromArray(array("font" => array( "bold" => true)));
			$i++;
			foreach($dataPendapatan as $eachPendapatan){
				$sheet->setCellValue('B'.$i, "* ".$eachPendapatan["AKUN"]);
				$sheet->setCellValue('D'.$i, $eachPendapatan["JUMLAH"]);
				
				$jumlah_pendapatan = $jumlah_pendapatan + $eachPendapatan["JUMLAH"];
				$i++;
			}
			
			$sheet->setCellValue('D'.$i, $jumlah_pendapatan)->getStyle("D".$i)->applyFromArray(array("font" => array( "bold" => true),'borders' => array('top' => array('style' => PHPExcel_Style_Border::BORDER_THICK))));
			$i++;
			
			$i++;
			$sheet->setCellValue('A'.$i, 'III. BIAYA-BIAYA')->getStyle("A".$i)->applyFromArray(array("font" => array( "bold" => true)));
			$i++;
			foreach($dataBeban as $eachBeban){
				$sheet->setCellValue('B'.$i, "* ".$eachBeban["AKUN"]);
				$sheet->setCellValue('C'.$i, $eachBeban["JUMLAH"]);
				
				$jumlah_beban = $jumlah_beban + $eachBeban["JUMLAH"];
				$i++;
			}
			
			$sheet->setCellValue('C'.$i,'')->getStyle("C".$i)->applyFromArray(array('borders' => array('top' => array('style' => PHPExcel_Style_Border::BORDER_THICK))));
			$sheet->setCellValue('D'.$i, $jumlah_beban)->getStyle("D".$i)->applyFromArray(array("font" => array( "bold" => true),'borders' => array('bottom' => array('style' => PHPExcel_Style_Border::BORDER_THICK))));
			$i++;
			
			$i++;
			$sheet->setCellValue('D'.$i, $jumlah_pendapatan-$jumlah_beban)->getStyle("D".$i)->applyFromArray(array("font" => array( "bold" => true),'borders' => array('bottom' => array('style' => PHPExcel_Style_Border::BORDER_DOUBLE))));
			
			
			$filename = "Laporan Laba Rugi per ".$_POST['tanggal_awal']." - ".$_POST['tanggal_akhir'];
			header('Content-Type: application/vnd.ms-excel');
			header('Content-Disposition: attachment;filename="'.$filename.'.xlsx"');
			header('Cache-Control: max-age=0');
			
			$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);

			$objWriter->save('php://output');
			unset($this->objWriter);
			unset($this->objWorksheet);
			unset($this->objReader);
			unset($this->objPHPExcel);
			exit();
			
		}
		$this->render('form_laporan_labarugi');
	}
	
	public function countBunga($id,$bayar){
		$pinjaman = Pinjaman::model()->find("ID_PINJAMAN=".$id);
		$bunga = $pinjaman->POKOK_PINJAMAN*0.1;
		$cicilan_raw = $pinjaman->POKOK_PINJAMAN / $pinjaman->TERMINE;
		$selisih = $bayar - ($cicilan_raw + $bunga);
		
		$selisih_bunga = $selisih + $bunga; 
		$selisih_bunga = floor($selisih_bunga/100)*100;
		$data = array("selisih_bunga"=>$selisih_bunga,"id_asal"=>$pinjaman->ASAL_PINJAMAN);
		
		return $data;
	}
	
	public function countLabaRugi($jurnal,$tanggal){
		foreach($jurnal as $keyx){
			$id_akun = $keyx->ID_AKUN;
			$saldo_awal = $keyx->SALDO_AWAL;
			$model_jurnal = JurnalDetail::model()->with('jurnal','master')->findAll("(jurnal.TANGGAL BETWEEN '".$tanggal['awal']."' AND '".$tanggal['akhir']."') AND t.ID_AKUN=".$id_akun);
			$jumlah = $saldo_awal;
			
			foreach ($model_jurnal as $each){
				$jumlah = $jumlah + $each->NILAI;
			}
			
			$array = array(
				"AKUN"=>$keyx->NAMA_AKUN,
				"JUMLAH"=>$jumlah
			);
			$data[]=$array;
		}
		
		return $data;
	}
	
	
	public function actionLaporanBukuBesar(){
		$model= new MasterAkun;
		if (isset($_POST['tanggal_awal'])&&isset($_POST['tanggal_akhir'])&&isset($_POST['MasterAkun'])){
			$tanggal_awal =date("Y-m-d", strtotime($_POST['tanggal_awal']));
			$tanggal_akhir =date("Y-m-d", strtotime($_POST['tanggal_akhir']));
			
			$trans= Jurnal::model()->with('detail')->findAll("detail.ID_AKUN=".$_POST['MasterAkun']['NAMA_AKUN']." AND TANGGAL BETWEEN '".$tanggal_awal."' AND '".$tanggal_akhir."'");
			
			$akun = MasterAkun::model()->find("ID_AKUN=".$_POST['MasterAkun']['NAMA_AKUN']);
			
			$objPHPExcel = new PHPExcel();
			$sheet = $objPHPExcel->setActiveSheetIndex(0);  
			
			$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(5);
			$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(15);
			$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(50);
			$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);
			$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(20);
			
			$sheet->mergeCells('A1:E1')->setCellValue('A1', 'BUKU BESAR '.$akun->NAMA_AKUN)->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
			$sheet->mergeCells('A2:E2')->setCellValue('A2', 'KOPERASI PEGAWAI RSU HAJI SURABAYA')->getStyle('A2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

			$sheet->mergeCells('A3:E3')->setCellValue('A3', 'PER : '.$_POST['tanggal_awal']." s/d ".$_POST['tanggal_akhir'])->getStyle('A3')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
			
			$sheet->getStyle("A1:A3")->applyFromArray(array("font" => array( "bold" => true, "size"=>18)));			
			$sheet->getStyle("A6:E6")->applyFromArray(array("font" => array( "bold" => true)));
			
			$sheet->setCellValue('A6', 'NO.')->getStyle('A6')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
			$sheet->setCellValue('B6', 'NO.JURNAL')->getStyle('B6')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
			$sheet->setCellValue('C6', 'NAMA TRANSAKSI')->getStyle('C6')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
			$sheet->setCellValue('D6', 'DEBIT')->getStyle('D6')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
			$sheet->setCellValue('E6', 'KREDIT')->getStyle('E6')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
			
			$sheet->getStyle("A6:E6")->applyFromArray(array('borders' => array('allborders' => array('style' => PHPExcel_Style_Border::BORDER_THICK))));
			
			$i=7;$j=1;$total_debit=0;$total_kredit=0;
			foreach($trans as $each){
				// var_dump($each->NO_JURNAL);
				// var_dump($each->detail[0]->ID_AKUN);
				// var_dump($each->TANGGAL);
				// var_dump($each->KETERANGAN);
				// var_dump($each->detail[0]->DEBIT_KREDIT);
				// var_dump($each->detail[0]->NILAI);
				
				$sheet->setCellValue('A'.$i, $j)->getStyle('A'.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
				$sheet->setCellValue('B'.$i, $each->NO_JURNAL)->getStyle('B'.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
				$sheet->setCellValue('C'.$i, $each->KETERANGAN);
				
				if($each->detail[0]->DEBIT_KREDIT == 1){
					$debit = $each->detail[0]->NILAI;
					$total_debit = $total_debit + $debit;
					$kredit = NULL;
				}
				else{
					$kredit = $each->detail[0]->NILAI;
					$total_kredit = $total_kredit + $kredit;
					$debit = NULL;
				}
				
				$sheet->setCellValue('D'.$i, $debit)->getStyle('D'.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
				$sheet->getStyle('D'.$i)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_NUMBER_COMMA_SEPARATED1);
				
				$sheet->setCellValue('E'.$i, $kredit)->getStyle('E'.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
				$sheet->getStyle('E'.$i)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_NUMBER_COMMA_SEPARATED1);
				
				$sheet->getStyle("A".$i.":E".$i)->applyFromArray(array('borders' => array('allborders' => array('style' => PHPExcel_Style_Border::BORDER_THIN))));
				
				$i++;$j++;
			}
			
			$total = $total_debit-$total_kredit;
			
			$sheet->mergeCells('A'.$i.':C'.$i)->setCellValue('A'.$i, 'TOTAL')->getStyle('A'.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
			$sheet->mergeCells('D'.$i.':E'.$i)->setCellValue('D'.$i, $total)->getStyle('D'.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
			$sheet->getStyle('D'.$i)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_NUMBER_COMMA_SEPARATED1);
			
			$sheet->getStyle("A".$i.":E".$i)->applyFromArray(array('borders' => array('allborders' => array('style' => PHPExcel_Style_Border::BORDER_THICK))));
			
			$sheet->getStyle("A".$i.":E".$i)->applyFromArray(array("font" => array( "bold" => true)));
			
			$filename = "Buku Besar ". $akun->NAMA_AKUN ." per ".$_POST['tanggal_awal']." - ".$_POST['tanggal_akhir'];
			header('Content-Type: application/vnd.ms-excel');
			header('Content-Disposition: attachment;filename="'.$filename.'.xlsx"');
			header('Cache-Control: max-age=0');
			
			$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);

			$objWriter->save('php://output');
			unset($this->objWriter);
			unset($this->objWorksheet);
			unset($this->objReader);
			unset($this->objPHPExcel);
			exit();
		}
		$this->render('form_laporan_bukubesar',array(
			'model'=>$model,
		));
	}
	
	public function actionLaporanShu()
	{
		if(isset($_POST['tanggal_awal'])&&isset($_POST['tanggal_akhir'])){
			
			$tanggal_awal =date("Y-m-d", strtotime($_POST['tanggal_awal']));
			$tanggal_akhir =date("Y-m-d", strtotime($_POST['tanggal_akhir']));
			$tanggal['awal']= $tanggal_awal;
			$tanggal['akhir']= $tanggal_akhir;
			
			$bayar = PembayaranPinjaman::model()->findAll("TANGGAL_TRANSAKSI BETWEEN '".$tanggal_awal."' AND '".$tanggal_akhir."'");
		
			foreach ($bayar as $databayar){
				$data [] = $this->countBunga($databayar->ID_PINJAMAN,$databayar->JUMLAH_BAYAR);
			}
			
			$asalpinjaman = AsalPinjaman::model()->findAll();
			foreach($asalpinjaman as $each){
				$bungaPinjaman = 0;
				foreach($data as $datum){
					if ($datum["id_asal"] == $each->ID_ASAL_PINJAMAN){
						$bungaPinjaman = $bungaPinjaman + $datum["selisih_bunga"];
					}
				}
				$dataBunga []= array("asal"=>$each->ASAL_PINJAMAN,"bunga"=>$bungaPinjaman);
			}
			
			$akun_pendapatan = MasterAkun::model()->findAll("ID_KEL_AKUN='4' OR ID_KEL_AKUN LIKE '4%'");
			$dataPendapatan= $this->countLabaRugi($akun_pendapatan,$tanggal);
			
			$akun_beban = MasterAkun::model()->findAll("ID_KEL_AKUN='5' OR ID_KEL_AKUN LIKE '5%'");
			$dataBeban= $this->countLabaRugi($akun_beban,$tanggal);
			
			$jumlah_pendapatan=0;$jumlah_beban=0;
			
			foreach($dataBunga as $eachBunga){
				$jumlah_pendapatan = $jumlah_pendapatan + $eachBunga["bunga"];
			}
			foreach($dataPendapatan as $eachPendapatan){
				$jumlah_pendapatan = $jumlah_pendapatan + $eachPendapatan["JUMLAH"];
			}
			foreach($dataBeban as $eachBeban){
				$jumlah_beban = $jumlah_beban + $eachBeban["JUMLAH"];
			}
			
			$labarugi = $jumlah_pendapatan-$jumlah_beban;
			
			// var_dump($labarugi);
			$this->layout='none';
			$this->render('laporan_shu',array('tanggal'=>$tanggal,'labarugi'=>$labarugi));
			
		}
		else 
		$this->render('form_laporan_shu');
	}
	
	public function actionTutupBuku()
	{
		if(isset($_POST['tahun'])){
			
			
		}
		else 
		$this->render('form_tutup_buku');
	}
}
?>